home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / mesa-amiwin / src / interp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-30  |  8.0 KB  |  337 lines

  1. /* interp.c */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  1.2
  6.  * Copyright (C) 1995  Brian Paul  (brianp@ssec.wisc.edu)
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25. $Id: interp.c,v 1.7 1995/10/23 21:27:54 brianp Exp $
  26.  
  27. $Log: interp.c,v $
  28.  * Revision 1.7  1995/10/23  21:27:54  brianp
  29.  * new GLubyte interpolation using fixed point arithmetic
  30.  *
  31.  * Revision 1.6  1995/06/12  15:43:57  brianp
  32.  * separate GLint and GLubyte interpolation functions
  33.  *
  34.  * Revision 1.5  1995/06/02  13:58:36  brianp
  35.  * faster gl_interpolate(), tried gl_interpolate_rgba()
  36.  *
  37.  * Revision 1.4  1995/05/22  21:02:41  brianp
  38.  * Release 1.2
  39.  *
  40.  * Revision 1.3  1995/03/17  19:15:21  brianp
  41.  * tried to improve gl_interp_texcoords, not much luck
  42.  *
  43.  * Revision 1.2  1995/03/04  19:29:44  brianp
  44.  * 1.1 beta revision
  45.  *
  46.  * Revision 1.1  1995/02/27  22:20:13  brianp
  47.  * Initial revision
  48.  *
  49.  */
  50.  
  51.  
  52. #include "context.h"
  53. #include "macros.h"
  54.  
  55.  
  56. /*#define TEST*/
  57.  
  58.  
  59. /*
  60.  * Linear integer interpolation:
  61.  * Iterpolate n integer values between y0 and y1 and put into yspan.
  62.  * When finished, yspan[0] = y0, yspan[n-1] = y1, and the rest of
  63.  * yspan[] is filled with interpolated values.
  64.  */
  65. void gl_interpolate_i( GLint n, GLint y0, GLint y1, GLint yspan[] )
  66. {
  67.    switch (n) {
  68.       case 1:
  69.          yspan[0] = y0;
  70.      return;
  71.       case 2:
  72.          yspan[0] = y0;
  73.          yspan[1] = y1;
  74.      return;
  75.       case 3:
  76.          yspan[0] = y0;
  77.      yspan[1] = (y0+y1) >> 1;
  78.          yspan[2] = y1;
  79.      return;
  80.       default:
  81.      if (y0==y1) {
  82.         register GLint i;
  83.         for (i=0;i<n;i++) {
  84.            yspan[i] = y0;
  85.         }
  86.      }
  87.      else {
  88.         register GLint i;
  89.         register GLint dx, dy;
  90.         register GLint a, b, d;
  91.         register GLint y;
  92.         register GLint qa, qb;
  93.            dx = n-1;
  94.         dy = y1 - y0;
  95.         qa = dy / dx;
  96.         dy = dy % dx;
  97.         if (dy<0) {
  98.            dy = -dy;
  99.            qb = qa - 1;
  100.         }
  101.         else {
  102.            qb = qa + 1;
  103.         }
  104.         a = dy+dy;   d = a-dx;   b = d-dx;
  105.         y = y0;
  106.         for (i=0;i<n;i++) {
  107.            yspan[i] = y;
  108.            if (d<0) {
  109.           d += a;
  110.           y += qa;
  111.            }
  112.            else {
  113.           d += b;
  114.           y += qb;
  115.            }
  116.         }
  117.      }
  118.    }
  119. }
  120.  
  121.  
  122.  
  123.  
  124. void gl_interpolate_ub( GLint n, GLint y0, GLint y1, GLubyte yspan[] )
  125. {
  126.    int i, dy;
  127.  
  128.    switch (n) {
  129.       case 1:
  130.          yspan[0] = y0;
  131.          return;
  132.       case 2:
  133.          yspan[0] = y0;
  134.          yspan[1] = y1;
  135.          return;
  136.       case 3:
  137.          yspan[0] = y0;
  138.          yspan[1] = (y0+y1) >> 2;
  139.          yspan[2] = y1;
  140.          return;
  141.       default:
  142.          y0 = y0 << 8;
  143.          y1 = y1 << 8;
  144.          dy = (y1-y0) / (n-1);
  145.          for (i=0;i<n;i++) {
  146.             yspan[i] = y0 >> 8;
  147.             y0 += dy;
  148.          }
  149.          return;
  150.    }
  151. }
  152.  
  153.  
  154.  
  155. void gl_interpolate_4ub( GLint n,
  156.                          GLint a0, GLint a1, GLubyte aspan[],
  157.                          GLint b0, GLint b1, GLubyte bspan[],
  158.                          GLint c0, GLint c1, GLubyte cspan[],
  159.                          GLint d0, GLint d1, GLubyte dspan[] )
  160. {
  161.    GLint i, m;
  162.    GLint da, db, dc, dd;
  163.  
  164.    switch (n) {
  165.       case 1:
  166.          aspan[0] = a0;
  167.          bspan[0] = b0;
  168.          cspan[0] = c0;
  169.          dspan[0] = d0;
  170.      return;
  171.       case 2:
  172.          aspan[0] = a0;   aspan[1] = a1;
  173.          bspan[0] = b0;   bspan[1] = b1;
  174.          cspan[0] = c0;   cspan[1] = c1;
  175.          dspan[0] = d0;   dspan[1] = d1;
  176.      return;
  177.       case 3:
  178.          aspan[0] = a0;   aspan[1] = (a0+a1)>>2;   aspan[2] = a1;
  179.          bspan[0] = b0;   bspan[1] = (b0+b1)>>2;   bspan[2] = b1;
  180.          cspan[0] = c0;   cspan[1] = (c0+c1)>>2;   cspan[2] = c1;
  181.          dspan[0] = d0;   dspan[1] = (d0+d1)>>2;   dspan[2] = d1;
  182.          return;
  183.       default:
  184.          a0 = a0 << 8;
  185.          b0 = b0 << 8;
  186.          c0 = c0 << 8;
  187.          d0 = d0 << 8;
  188.          m = n-1;
  189.          da = ((a1<<8)-a0) / m;
  190.          db = ((b1<<8)-b0) / m;
  191.          dc = ((c1<<8)-c0) / m;
  192.          dd = ((d1<<8)-d0) / m;
  193.          for (i=0;i<n;i++) {
  194.             aspan[i] = a0 >> 8;    a0 += da;
  195.             bspan[i] = b0 >> 8;    b0 += db;
  196.             cspan[i] = c0 >> 8;    c0 += dc;
  197.             dspan[i] = d0 >> 8;    d0 += dd;
  198.          }
  199.          return;
  200.    }
  201. }
  202.  
  203.  
  204.  
  205.  
  206. #ifndef TEST
  207.  
  208. /*
  209.  * Perform texture coordinate interpolation along a line in window coord-
  210.  * inate space.  Depending on the perspective correction hint we'll either
  211.  * just do simple linear interpolation or interpolation with perspective
  212.  * correction.
  213.  * Input:  n - number of texture coords to produce
  214.  *         eyez0, eyez1 - z coordinate of end points in eye coords
  215.  *         winz0  winz1 - z coordinate of end points in window coords
  216.  *         s0, s1 - S-component of texture coords at end points
  217.  *         t0, t1 - T-component of texture coords at end points
  218.  * Output:  s, t - resulting arrays of S and T texture coordinates
  219.  *          z - interpolated Z component of eye coordinates along the line
  220.  *              (note that z can be a NULL pointer if z isn't needed)
  221.  */
  222. void gl_interp_texcoords( GLuint n,
  223.               GLfloat eyez0, GLfloat eyez1,
  224.               GLfloat winz0, GLfloat winz1,
  225.               GLfloat s0, GLfloat s1,
  226.               GLfloat t0, GLfloat t1,
  227.               GLfloat s[], GLfloat t[], GLfloat *z )
  228. {
  229.    /* TODO: the main loop in this function could be optimized with */
  230.    /* forward differences, etc.  However, since texture mapping is */
  231.    /* so expensive, optimizing this might not make a big difference. */
  232.  
  233.    /* TODO: this function has a terrible problem with numerical error */
  234.    /* under certain conditions, namely when interpolating along a vector */
  235.    /* which is _nearly_ parallel to the viewplane. */
  236.  
  237.    GLfloat d = CC.ProjectionMatrix[14];
  238.    GLfloat c = CC.ProjectionMatrix[10];
  239.    GLfloat delta_eyez = eyez1 - eyez0;
  240.    GLfloat delta_winz = winz1 - winz0;
  241.    GLfloat delta_s = s1 - s0;
  242.    GLfloat delta_t = t1 - t0;
  243.    GLuint i;
  244.  
  245.    if (n==1) {
  246.       s[0] = s0;
  247.       t[0] = t0;
  248.       if (z)  z[0] = eyez0;
  249.       return;
  250.    }
  251.  
  252.    if (CC.Hint.PerspectiveCorrection==GL_NICEST && d!=0.0
  253.        && ABS(delta_eyez)>0.001) {
  254.       /* interpolate with perspective compensation */
  255.       GLfloat q, r, winz, ndcz, eyez;
  256.  
  257.       for (i=0;i<n;i++) {
  258.      q = (GLfloat) i / (n-1);
  259.      winz = (winz0 + q * delta_winz) / (GLfloat) MAX_DEPTH;
  260.  
  261.      ndcz = (winz - CC.Viewport.Tz) / CC.Viewport.Sz;
  262.  
  263.      eyez = -d / (c+ndcz);
  264.  
  265.      r = (eyez - eyez0) / delta_eyez;
  266.      r = CLAMP( r, 0.0, 1.0 );    /* to catch over/underflow */
  267.  
  268.      s[i] = s0 + r * delta_s;
  269.      t[i] = t0 + r * delta_t;
  270.      if (z) {
  271.         z[i] = eyez;
  272.      }
  273.       }
  274.    }
  275.    else {
  276.       /* simple linear interpolation */
  277.       if (z) {
  278.      for (i=0;i<n;i++) {
  279.         GLfloat q = (GLfloat) i / (n-1);
  280.         s[i] = s0 + q * delta_s;
  281.         t[i] = t0 + q * delta_t;
  282.         z[i] = eyez0 + q * delta_eyez;
  283.      }
  284.       }
  285.       else {
  286.      for (i=0;i<n;i++) {
  287.         GLfloat q = (GLfloat) i / (n-1);
  288.         s[i] = s0 + q * delta_s;
  289.         t[i] = t0 + q * delta_t;
  290.      }
  291.       }
  292.    }
  293. }
  294.  
  295. #endif
  296.  
  297.  
  298. #ifdef TEST
  299. static void foo( int n, int red[], int green[], int blue[], int alpha[] )
  300. {
  301.  
  302. }
  303.  
  304.  
  305.  
  306. #define ITER 700
  307. #define MAX 500
  308.  
  309. #include "interp.h"
  310.  
  311. main()
  312. {
  313.    int i, j, n, jj;
  314.  
  315.    for (i=0;i<ITER;i++) {
  316.       int red[MAX], green[MAX], blue[MAX], alpha[MAX];
  317.       jj = MAX2( 2, i );
  318.       for (j=0;j<jj;j++) {
  319.      n = j/4+1;
  320.  
  321.      GL_INTERPOLATE( n, j, n, red );
  322.      GL_INTERPOLATE( n, j+3, j*8, green );
  323.      GL_INTERPOLATE( n, i, j+i*2, blue );
  324.      GL_INTERPOLATE( n, i, i, alpha );
  325. /*     gl_interpolate_rgba( n, j, n, red,
  326.                              j+3, j*8, green,
  327.                                  i, j+i*2, blue,
  328.                                  i, i, alpha );
  329. */
  330.       }
  331.       foo( n, red, green, blue, alpha );
  332.    }
  333.  
  334. }
  335.  
  336. #endif
  337.